import 'package:chathouse/constant/app_constant.dart';
import 'package:chathouse/pages/expert/buyWallet/recharge_bank_card.dart';
import 'package:flutter/material.dart';

import '../../public/event_bus.dart';

class RootRouteController extends GetxController {
  //一級控制
  late PageController pageController;
  //頁面存儲器
  List<PageControl> subRoute = [];
  //刷新控制
  var refreshNum = 0.obs;

  RootRouteController(int initialIndex, List<Widget> initSubPage) {
    pageController = PageController(initialPage: initialIndex);
    subRoute = initSubPage.map((e) {
      return PageControl(e);
    }).toList();
  }

  @override
  void onClose() {
    pageController.dispose();
    super.onClose();
  }

  //返回Body
  Widget getRootPageBody() {
    return SafeArea(
      child: PageView(
        physics: const NeverScrollableScrollPhysics(),
        controller: pageController,
        children: subRoute.map((e) {
          return Obx(() {
            refreshNum.value;
            return IndexedStack(children: e.subWidgets, index: e.currentIndex);
          });
        }).toList(),
      ),
    );
  }

  //一級導航控制
  void jumpToIndex(int index) {
    pageController.jumpToPage(index);
  }

  //二級入棧
  void addPage(Widget route) {
    PageControl pageControl = subRoute[pageController.page!.toInt()];
    pageControl.subWidgets.add(route);
    pageControl.currentIndex = pageControl.subWidgets.length - 1;
    refreshNum++;
  }

  //二級退出
  void removePage() {
    PageControl pageControl = subRoute[pageController.page!.toInt()];
    if (pageControl.subWidgets.length <= 1) return;
    pageControl.subWidgets.removeLast();
    pageControl.currentIndex = pageControl.subWidgets.length - 1;
    refreshNum++;
  }

  ///前往各自首頁
  void cleanSubPageAndGoFirstPage(int index) {
    PageControl pageControl = subRoute[index];
    if (pageControl.subWidgets.length <= 1) return;
    pageControl.subWidgets = [pageControl.subWidgets.first];
    pageControl.currentIndex = 0;
    refreshNum++;
  }

  ///客戶端用:
  ///前往客戶端訂單頁
  void goClientOrderPage() {
    pageController.jumpToPage(1);
    eventBus.fire(UpdateClientOrderEvent(0));
  }
}

class PageControl {
  int currentIndex = 0;
  Widget currentWidget;
  List<Widget> subWidgets = [];
  PageControl(this.currentWidget) {
    subWidgets = [currentWidget];
  }
}

///導航控制
mixin NavigationHelper {
  ///導航控制
  ///route 進去的頁面
  ///hasNavBottomBar,true:用自己寫的，false:用 Get.to 控制
  ///onResult hasNavBottomBar為false時生效,用於頁面返回數據
  void navigateTo(Widget route,
      {bool hasNavBottomBar = true, Function(dynamic result)? onResult}) {
    if (hasNavBottomBar) {
      RootRouteController clientRootPageController = AppConstant.rootController;
      Widget widget = WillPopScope(
        onWillPop: () {
          back();
          return Future.value(false);
        },
        child: route,
      );
      clientRootPageController.addPage(widget);
      return;
    }
    Get.to(route)?.then(
      (value) {
        return onResult?.call(value);
      },
    );
  }

  ///導航控制
  ///hasNavBottomBar,true:用自己寫的，false:用 Get.back 控制
  ///backData hasNavBottomBar為false時生效,用於頁面返回數據
  void back({bool hasNavBottomBar = true, dynamic backData}) async {
    FocusManager.instance.primaryFocus?.unfocus();
    if (hasNavBottomBar) {
      RootRouteController clientRootPageController = AppConstant.rootController;
      clientRootPageController.removePage();
      return;
    }
    Get.back(result: backData);
  }
}
